$(function(){
    chrome.storage.sync.get('total',function(budget){
        var totalAmount = 0;
        if(budget.total){
            totalAmount = parseFloat(budget.total);
        }
        $('#total').text(totalAmount);
    });
    $('#add').click(function(){
        chrome.storage.sync.get('total',function(budget){
            
            var totalAmount = 0;
            if(budget.total){
                totalAmount = parseFloat(budget.total);
            }
            var amount = $('#amount').val();
            if(amount){
                totalAmount = totalAmount + parseFloat(amount);
                chrome.storage.sync.set({'total':totalAmount})
            }
            $('#total').text(totalAmount);
            $('#amount').val('');
        })
    })


    



    /* ----------------解析xml到chrome.storage.sync.set({'xls_data':xls_data})-------------------- */
    var fromTo = '';
    let fileName = ''
    $("#get_file_data").change(function () {
        let reader = new FileReader();
        let file = this.files[0];
        let workbook;
        fileName = file.name;
        reader.readAsBinaryString(file);
        reader.onload = function (e) {
            let data = e.target.result;
            workbook = XLSX.read(data, { type: 'binary' });
            let xls_data =[];
            // 遍历每张表读取
            for (let sheet in workbook.Sheets) {
                if (workbook.Sheets.hasOwnProperty(sheet)) {
                    fromTo = workbook.Sheets[sheet]['!ref'];//读取的全部数据 A1:AN203
                    console.log(fromTo);
                    xls_data = xls_data.concat(XLSX.utils.sheet_to_json(workbook.Sheets[sheet], { header: 1, defval: '' }));//合并数据
                    break; // 如果只取第一张表，就取消注释这行
                }
            }
            console.log(xls_data);
            chrome.storage.sync.set({'xls_data':xls_data})
            let notifyOptions = {
                /* 弹窗提醒，需要在配置文件中申请notifications权限
                type:basic image progress list
                */
                type:'basic',
                title:'数据解析完毕',
                iconUrl:'../images/16.png',
                message:JSON.stringify(xls_data)
            }
            chrome.notifications.create('notifyId',notifyOptions)
            /* 图标徽标 */
                //V3版本后设置badge已改成 => 
                chrome.action.setBadgeText({ text: '解析' });
        }
    })

    /* -----向content.js发送数据-将数据更新到当前页面中-------- */
    $('#reload_data').click(function(){
        chrome.storage.sync.get('xls_data',function(budget){
            
            var xls_data = [];
            if(budget.xls_data){
                xls_data = budget.xls_data;
            }
            console.log(xls_data)

            //下面代码是向浏览器的当前tab发送一个消息，消息由main.js中的监听器接收
            chrome.tabs.query({active: true, currentWindow: true}, function(tabs){  
                chrome.tabs.sendMessage(tabs[0].id,{"xls_data":xls_data}, function(response){
                    console.log(response);
                });  
            });

        })
    })
})